package zouhaotian.rbac.view;

import zouhaotian.rbac.domain.dto.RoleDto;
import zouhaotian.rbac.domain.enums.RoleDateScope;
import zouhaotian.rbac.domain.vo.Result;
import zouhaotian.rbac.domain.vo.Rolevo;
import zouhaotian.rbac.exception.ExceptionHandler;
import zouhaotian.rbac.service.IRoleService;
import zouhaotian.rbac.service.impl.cache.RoleService;
import zouhaotian.rbac.util.DateTimeUtils;
import zouhaotian.rbac.util.SingletonFactory;

/**
 * @auther 邹昊天
 * @since 2025/6/19 23:49
 */
public class RoleView extends BaseView<Rolevo, IRoleService>{

    public static final String MENU = """
            --------------------
                  角色管理
            --------------------
               | 0...返回上级 |
               | 1...角色列表 |
               | 2...创建角色 |
               | 3...更新角色 |
               | 4...删除角色 |
            --------------------
            请输入选项:""";


    private RoleView() {
        service = SingletonFactory.getInstance(RoleService.class);
    }

    /**
     * 显示界面
     */
    public void show() {
        do {
            try {
                System.out.print(MENU);
                String menuChoice = MainView.input.nextLine();
                switch (menuChoice) {
                    case "0" -> {
                        System.out.println("已返回上一级菜单");
                        return;
                    }
                    case "1" -> list();
                    case "2" -> create();
                    case "3" -> update();
                    case "4" -> delete();
                    default -> System.out.println("输入非法，请输入正确菜单编号！");
                }
            }catch (Exception e){
                ExceptionHandler.handle(e);
            }
        } while (true);
    }


    public void list() {
        Result result = service.list();
        cache = (Rolevo[]) result.data;
        System.out.println("ID\t角色名称\t角色数据访问范围\t更新时间");
        for (Rolevo role : cache) {
            System.out.printf("%d\t%s\t%s\t%s\n", role.id, role.name, role.dataScope, DateTimeUtils.format(role.updateAt));
        }
    }

    public void create() {
        System.out.print("请输入角色名称：");
        String name = MainView.input.nextLine();
        System.out.println("请输入角色数据访问范围：(个人，部门，全局)之一");
        RoleDateScope dataScope = null;
        do {
            String str = MainView.input.nextLine();
            try{
                dataScope = Enum.valueOf(RoleDateScope.class, str);
            } catch (Exception e){
                System.out.print("输入的值不合法，请重新输入");
            }

        } while (dataScope == null);
        RoleDto dto = new RoleDto(name, dataScope);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {
        Rolevo role = getCacheById();

        if (role == null) {
            return;
        }

        System.out.print("请输入角色名称，按回车表示不修改");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? role.name : name;
        System.out.print("请输入角色数据访问范围，输入应为[个人,部门,全局]其中之一");
        RoleDateScope dataScope = null;
        do {
            String str = MainView.input.nextLine();
            if ("".equals(str)){
                dataScope = role.dataScope;
                break;
            }
            try{
                dataScope = Enum.valueOf(RoleDateScope.class, str);
            } catch (Exception e){
                System.out.print("输入的值不合法，请重新输入");
            }

        } while (dataScope == null);
        RoleDto dto = new RoleDto(name, dataScope);
        Result result = service.update(dto, role.id);
        if (!result.success) {
            printError(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

    public void delete() {
        Rolevo role = getCacheById();

        if (role == null) {
            return;
        }

        System.out.println("输入'Y'确认删除，输入其他取消删除");
        String confirm = MainView.input.nextLine();

        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消删除");
            return;
        }

        Result result = service.delete(role.id);

        if (!result.success) {
            System.out.println(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

}

